define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            // Table.api.init({
            //     extend: {
            //         index_url: 'coll/anal/index' + location.search,
            //         add_url: 'coll/anal/add',
            //         edit_url: 'coll/anal/edit',
            //         del_url: 'coll/anal/del',
            //         multi_url: 'coll/anal/multi',
            //         table: 'analysis',
            //     }
            // });

            // var table = $("#table");

            // // 初始化表格
            // table.bootstrapTable({
            //     url: $.fn.bootstrapTable.defaults.extend.index_url,
            //     pk: 'id',
            //     sortName: 'id',
            //     columns: [
            //         [
            //             {checkbox: true},
            //             {field: 'id', title: __('Id')},
            //             {field: 'source', title: __('Source')},
            //             {field: 'cost', title: __('Cost'), operate:'BETWEEN'},
            //             {field: 'operator', title: __('Operator')},
            //             {field: 'company', title: __('Company')},
            //             {field: 'status', title: __('Status')},
            //             {field: 'examine_name', title: __('Examine_name')},
            //             {field: 'is_income', title: __('Is_income')},
            //             {field: 'controller', title: __('Controller')},
            //             {field: 'source_id', title: __('Source_id')},
            //             {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
            //         ]
            //     ]
            // });
            $('.btn-res').click(function () {
                // var url = Config.this_url+'/coll/summary/results'
                // layer.open({
                //     type: 2, // 1-在content中写html代码， 2-在content中写src路径
                //     area: ['100%', '100%'],
                //     title: "汇总",
                //     content: url,
                //     // content: Config.this_url+'/purchase/requisition',
                // })
                layer.open({
                    type:2,
                        area: ['30%', '50%'],
                        title: "汇总",
                        content: Config.this_url+'/coll/summary/seach',
                })
            })

            // 为表格绑定事件
            // Table.api.bindevent(table);
            Controller.api.bindevent();
        },
        add: function () {
            Controller.api.bindevent();
        },
        detaillist: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'coll/summary/detaillist' + location.search,
                    // add_url: 'coll/summary/detaillist',
                    // edit_url: 'coll/summary/detaillist',
                    // del_url: 'coll/summary/detaillist',
                    multi_url: 'coll/summary/detaillist',
                    table: 'analysis',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),class:'did'},
                        {field: 'createtime', title: __('单据日期'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'number', title: __('单据编号')},
                        {field: 'type', title: __('单据类型')},
                        {field: 'enter_num', title: __('入库数量')},
                        {field: 'out_num', title: __('出库数量')},
                        {field: 'before_num', title: __('入/出库前余量')},
                        {field: 'now_num', title: __('库存余量')},
                        {field: 'final_cost', title: __('成本价')},
                        {field: 'price', title: __('库存金额')},
                        {field: 'store_name', title: __('仓库名称')},
                        {field: 'explain', title: __('摘要')},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });
            $('table').on('dblclick','td',function(){

                var oid = $(this).parents('tr').find('.did').html()
                console.log(oid)
                var url = Config.this_url+'/coll/summary/getdetail?ids='+oid
                $.post(url,{},function (res) {
                    layer.open({
                        type: 2, // 1-在content中写html代码， 2-在content中写src路径
                        area: ['80%', '90%'],
                        title: "采购详情",
                        content: Config.this_url+res.url,
                        // content: Config.this_url+'/purchase/requisition',
                    })
                })
                // var url = Config.this_url+'/purchase/order/detail?ids='+oid

            });
            Table.api.bindevent(table);
        },
        seach: function () {
            $('.btn-embossed').click(function () {
                var data = {
                    'starttime':$('#starttime').val(),
                    'endtime':$('#endtime').val(),
                    'pid':$('#c-pid').val(),
                    'storehouse_id':$('#c-storehouse_id').val(),
                    'admin_id':$('#c-admin_id').val(),
                    'goods_id':$('#c-goods_id').val(),
                }

                var index = parent.Layer.getFrameIndex(window.name);
                parent.layer.close(index);
                parent.openresults(data);

                return false;
            })
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        results: function () {
            f()
            function f() {
                layer.open({
                    type:2,
                    area: ['30%', '50%'],
                    title: "汇总",
                    content: Config.this_url+'/coll/summary/seach',
                })
            }
            $('.connnn').on('dblclick','tr',function () {
                console.log(datas)
                var turl = Config.this_url+'/coll/summary/detaillist?goods_id='+$(this).data('goodsid')+'&storehouse_id='+datas['storehouse_id']+'&starttime='+datas['starttime']+'&admin_id='+datas['admin_id']+'&endtime='+datas['endtime'];
                    Backend.api.addtabs(turl, '进销存明细表', 'fa fa-circle-o fa-fw');
            })
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});